package com.lagou.homework.modulefour.first;

import javax.print.DocFlavor;

/*
 * @className Worker
 * @description 员工类
 * @author admin
 * @date 2022/4/1 15:08
 * */
public class Worker {

    private String name;
    private int age;

    // 无参构造
    public Worker() {
    }

    // 有参构造
    public Worker(String name, int age) throws Exception{
        setName(name);
        setAge(age);
    }

    // get、set方法
    public void setName(String name) {
        if ("".equals(name) || null == name) {
            try{
                throw new NullPointerException();
            }catch(NullPointerException e){
                e.printStackTrace();
            }
        }
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        if (age < 18 || age > 150) {
            try {
                throw new WorkerException("年龄异常");
            } catch (WorkerException e) {
                e.printStackTrace();
            }
        }
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Worker{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
